package com.brave.page.service;

import com.brave.banner.repository.MCBannerInfoRepository;
import com.brave.base.BaseService;
import com.brave.base.MD5Kit;
import com.brave.base.RedisKit;
import com.brave.page.dto.DoRegisterDTO;
import com.brave.user.entity.MCUserBaseInfo;
import com.brave.user.entity.MCUserInviteMappingEntity;
import com.brave.user.repository.MCUserBaseInfoRepository;
import com.brave.user.repository.MCUserInviteMappingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class InviteService extends BaseService
{
    @Autowired
    private MCBannerInfoRepository bannerInfoRepository;
    @Autowired
    private MCUserInviteMappingRepository userInviteMappingRepository;
    @Autowired
    private MCUserBaseInfoRepository userBaseInfoRepository;
    @Autowired
    private RedisKit redisKit;
    
    
    
    public String doRegister(DoRegisterDTO dto)
    {
        Date now = new Date();
        //验证码是否正确
        String verifyCode = dto.getUserVerifyCode();
        String redisVerifyCode = redisKit.getValue(dto.getUserPhone().trim());
        if(!redisVerifyCode.equals(verifyCode))
        {
            return exception("验证码不合法或已过期.");
        }
        StringBuilder querySQL = new StringBuilder();
        querySQL.append("SELECT * FROM mc_user_base_info WHERE MD5(id) = '");
        querySQL.append(dto.getInviteToken()).append("'");
        List<Map<String, Object>> list = bannerInfoRepository.findList(querySQL.toString());
        //有效邀请人
        if(null != list && 0 < list.size())
        {
            Map<String, Object> userInfo = list.get(0);
            long inviteUserId = Long.parseLong(userInfo.get("id").toString());
            //创建用户
            String nickName = dto.getUserPhone().substring(0, 3);
            MCUserBaseInfo user = new MCUserBaseInfo();
            user.setUserName(String.format("%s%s%s", "用户", nickName, "****"));
            user.setLoginName(dto.getUserPhone());
            user.setNickName(dto.getUserNickName());
            user.setPassword(MD5Kit.MD5Encode(dto.getUserPassword()));
            user.setPhone(dto.getUserPhone());
            user.setToken("");
            user.setAge(0);
            user.setIdCard("");
            user.setHeadUrl("");
            user.setIdCardFront("");
            user.setHandIdCard("");
            user.setIdCardBack("");
            user.setSex("M");
            user.setDeviceType("AD");
            user.setDeviceId("");
            user.setCreditScore(BigDecimal.ZERO);
            user.setIsVip(0);
            user.setVipLevel(0);
            user.setLatitude(BigDecimal.ZERO);
            user.setLongitude(BigDecimal.ZERO);
            user.setBalance(BigDecimal.ZERO);
            user.setBeanBalance(BigDecimal.ZERO);
            user.setRedDiamond(0);
            user.setGreenDiamond(0);
            user.setBlueDiamond(0);
            user.setCreateDate(now);
            user.setLastLoginDate(now);
            user.setAlipay("");
            user.setWechat("");
            user.setWeibo("");
            user.setQrcodeUrl("");
            user.setRegionCode("");
            user.setDomainCode("");
            user.setComment("");
            long userId = userBaseInfoRepository.save(user).getId();
            
            //邀请关系创建
            MCUserInviteMappingEntity mapping = new MCUserInviteMappingEntity();
            mapping.setUserId(userId);
            mapping.setInviteUserId(inviteUserId);
            mapping.setCreateDate(now);
            userInviteMappingRepository.save(mapping);
            
            return success();
        }else
        {
            return exception("邀请码错误或邀请已过期.");
        }
    }
    
    
    
    
    
    
    
    
    
}